package com.example.neusoft.yuejian_12.Model;

import com.example.neusoft.yuejian_12.Interface.ClubFace;
import com.example.neusoft.yuejian_12.JavaBean.Club;
import com.example.neusoft.yuejian_12.Listener.ClubBListener;
import com.example.neusoft.yuejian_12.Listener.ClubCollectListener;
import com.example.neusoft.yuejian_12.Listener.ClubDetailListener;
import com.example.neusoft.yuejian_12.Listener.ClubListener;
import com.example.neusoft.yuejian_12.Listener.ClubStateListener;
import com.example.neusoft.yuejian_12.Listener.SearchListener;
import com.example.neusoft.yuejian_12.Listener.UserNoCollectListener;
import com.example.neusoft.yuejian_12.Service.ClubService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by admin on 2017/9/30.
 */

public class ClubModel implements ClubFace {
    private Retrofit retrofit;
    private ClubService clubService;
   private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址*/

    public ClubModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获得列表

     * @param listListener 监听对象
     */
    @Override
    public void getLists(String id, String club_name,final ClubBListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<List<Club>> call =clubService.ClubList();
        call.enqueue(new Callback<List<Club>>() {
            @Override
            public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Club>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


    @Override
    public void getList(String id, String club_name,final ClubListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<List<Club>> call =clubService.getClubList();
        call.enqueue(new Callback<List<Club>>() {
            @Override
            public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Club>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


//获取详情
    @Override
        public void getdetail(String id,final ClubDetailListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Club> call =clubService.getclubById("lgj98rfkiqvenhrk2o7oe2po81","1","Clubteacher","club",id);
        call.enqueue(new Callback<Club>() {
            @Override
            public void onResponse(Call<Club> call, Response<Club> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Club> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


//获取俱乐部课程
 @Override
    public void getclass(String id,final ClubDetailListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Club> call =clubService.getclassinfo("lgj98rfkiqvenhrk2o7oe2po81","1","Clubcourse","club",id);
        call.enqueue(new Callback<Club>() {
            @Override
            public void onResponse(Call<Club> call, Response<Club> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Club> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

//搜索
@Override
   public void getsearch(String club_name,final SearchListener listListener) {
    clubService = retrofit.create(ClubService.class);
       Call<List<Club>>  call =clubService.getSearchClub(club_name);
       call.enqueue(new Callback<List<Club>>() {
        @Override
        public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
            if (response.isSuccessful() && response.body() != null) {
                listListener.onResponse(response.body());
            } else {
                listListener.onFail("onresponse fail");
            }
        }

        @Override
        public void onFailure(Call<List<Club>> call, Throwable t) {
            listListener.onFail(t.toString());
        }
    });
}

    //获取俱乐部视频
    @Override
    public void getvideo(String id,final ClubDetailListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Club> call =clubService.getVideoinfo("lgj98rfkiqvenhrk2o7oe2po81","1","Clubvideo","club",id);
        call.enqueue(new Callback<Club>() {
            @Override
            public void onResponse(Call<Club> call, Response<Club> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Club> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

  //收藏俱乐部
    @Override
    public void CollectClub(String userid, String fan_id, final ClubCollectListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Integer> call =clubService.CollectClub("lgj98rfkiqvenhrk2o7oe2po81",userid,fan_id,"1");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() > 0) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    @Override
    public void getClubState(String fan_id, String userid, final ClubStateListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Integer> call =clubService.getClubState(fan_id,userid,"1","lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    listListener.onResponse(response.body());
                }else{
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void Nocollect(String user_id, String fan_id, final UserNoCollectListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Integer> call =clubService.Nocollect(user_id ,fan_id,"1");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
/*
    @Override
    public void uncollect(String userid, String fan_id, final ClubUncollectListener listListener) {
        clubService = retrofit.create(ClubService.class);
        Call<Integer> call =clubService.uncollect(userid,fan_id,"1");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    listListener.onResponse(response.body());
                }else{
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }*/
//  public retrofit2.Call<Integer> uncollect(String userid, String fan_id){
//     clubService = retrofit.create(ClubService.class);
//    return  clubService.uncollect(userid,fan_id,"1");
//  }
//    public retrofit2.Call<Integer> CollectClub(String userid, String fan_id){
//        clubService = retrofit.create(ClubService.class);
//        return  clubService.CollectClub("lgj98rfkiqvenhrk2o7oe2po81",userid,fan_id,"1");
//    }



}